package sec_pages;

import org.openqa.selenium.WebElement;
import utils.Log4Test;

import java.util.Set;

import static org.testng.Assert.assertTrue;


public class SecHomePage extends SecGeneralPage {

    public void open(String URL) {

        webDriver.get(URL);

        Log4Test.info("Open WebUrl " + URL);

    }

    public void clickEnterToShop() {

        elementIsLocated(getLocator("linkEnterToShop")).click();

        Log4Test.info("Click enter link to the shop ");

    }


    public void clickAndenterCredentialsAndLogin(String userEmail, String userPassword) {

        String parentWindowId = webDriver.getWindowHandle();

        elementIsLocated(getLocator("linkEnterUserFacebook")).click();

        Log4Test.info("User Facebbok Click enter link ");

        Set<String> allWindowsId = webDriver.getWindowHandles();

        if (!allWindowsId.isEmpty()) {

            for (String WindowId : allWindowsId) {
                webDriver.switchTo().window(WindowId);

                waitForPageLoaded(webDriver);

                if (webDriver.getPageSource().contains("Log in to use your Facebook account with")) {

                    elementIsLocated(getLocator("emailField")).clear();
                    elementIsLocated(getLocator("emailField")).sendKeys(userEmail);

                    elementIsLocated(getLocator("passwordField")).clear();
                    elementIsLocated(getLocator("passwordField")).sendKeys(userPassword);

                    elementIsLocated(getLocator("loginButton")).click();
                }

            }
        }
        assertTrue(webDriver.switchTo().window(parentWindowId).getTitle().contains("Интернет-магазин ROZETKA"));


        Log4Test.info("Enter Credentials And Login ");
    }


    public WebElement verifyFirstLastNameAppearedTop() {

        Log4Test.info("Verify that your First and Last Name appeared on the top right corner");

        return elementIsLocated(getLocator("userLink"));
    }

    public void clickFirstLastName() {

        elementIsLocated(getLocator("userLink")).click();

        Log4Test.info("Click your First and Last Name ");

    }

    public void searchItemInSearchField(String searchItem) {


        elementIsLocated(getLocator("searchField")).clear();
        elementIsLocated(getLocator("searchField")).sendKeys(searchItem);

        elementIsLocated(getLocator("searchButton")).click();

        Log4Test.info("Search Sony Xperia Z2 D6502 Black ");
    }
}
